﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.Common;
using System.Linq.Expressions;

namespace Neotic.DataAccess.Sql.Operations
{
    public class In : ISqlCondition
    {
        private ISelectColumn _leftColumn;
        private object[] _values;

        protected In() {}

        public In(ISelectColumn c, object[] vals)
        {
            if (c == null)
                throw new ArgumentNullException("Paramètre c attendu");

            if (vals == null || vals.Length == 0)
                throw new ArgumentNullException("Paramètre vals attendu");

            _leftColumn = c;
            _values = vals;
        }

        public virtual string ToSql(DbParameterCollection cmdParams)
        {
            string valuesPart = string.Empty;

            if (_values[0] is string)
                valuesPart = string.Concat("'", string.Join("', '", _values), "'");
            else
                valuesPart = string.Join(", ", _values);

            return string.Concat(_leftColumn.FullName, " IN (", valuesPart, ") ");
        }

        public ExpressionType? MyExpressionType { get { return null; } }

        public ISqlCondition ParseExpression(Expression<Func<ISqlCondition, object>> predicate)
        {
            throw new NotImplementedException();
        }
    }
}
